<?php
namespace App\Http\Controllers\admin_system;

use App\Common\ApiData;
use Aws\Common\Enum\Region;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\V1\ChapterModel;
use App\Models\V1\RegionModel;
use App\Models\V1\MemberModel;
use DB;

class ChapterController extends Controller{


    /**
     * 列表显示界面
     * */
    public function chapters(Request $request){
        $chapterModel   = new ChapterModel();
        $keywords       = trim($request->input('keywords'));
        $chapterList    = $chapterModel->get_chapters($keywords);

        foreach($chapterList as $chapter){
            $chapterId  = $chapter->chapter_id;
            $chapter->prisidentNum  = $chapterModel->count_prisident($chapterId);
            $chapter->drpNum        = $chapterModel->count_drp($chapterId);
            $chapter->memberNum     = $chapterModel->count_member($chapterId,0);
            $chapter->companyNum    = $chapterModel->count_company($chapterId);
        }
        return view('admin_system/chapter/chapters',['chapters'=>$chapterList,'keywords'=>$keywords]);
    }

    /**
     * 获得分会信息
     * */
    public function get_chapter_info(Request $request){

        $chapterId  = $request->input('chapterId');
        $chapterModel   = new ChapterModel();
        $chapterInfo    = $chapterModel->chapter_info($chapterId);

        $apiData    = new ApiData();
        $apiData->set_data('chapterInfo',$chapterInfo)->send(200,"ok");
    }



    /**
     * 编辑首页
     * */
    public function chapter_edit(Request $request)
    {
        $chapterId      = $request->input('chapterId',0);
        $chapterModel   = new ChapterModel();

        if($chapterId > 0)
        {
            $chapterInfo    = $chapterModel->chapter_info($chapterId);
        } else
        {
            $chapterInfo    = $chapterModel->get_empty_chapter();
        }
        $regionModel    = new RegionModel();
        $provinces      = $regionModel->get_provices();
        $provinceId     = $chapterInfo->province;
        $provinceId     = $provinceId > 0 ? $provinceId : $provinces[0]->region_id;
        $cities         = $regionModel->get_region($provinceId);
        $data   = [
            'chapter'   => $chapterInfo,
            'provinces' => $provinces,
            'cities'    => $cities
        ];
        return view('admin_system/chapter/chapter-edit',$data);
    }


    /**
     * 删除分会
     * */
    public function chapter_delete(Request $request){
        $chapterId = $request->input('chapterId');
        $chapterModel   = new ChapterModel();
        $chapterModel->delete_chapter($chapterId);
        TableLog()
            ->content(['is_delete'=>1])
            ->table('chapter')
            ->key($chapterId)
            ->save();

        return redirect()->back();
    }


    /**
     * 编辑操作界面
     * */
    public function chapter_edit_action(Request $request){
        $input    = $request->all();
        unset($input['_token']);
        $chapterModel   = new ChapterModel();
        $chapterModel->add_chapter($input);
        TableLog()
            ->content($input)
            ->table('chapter')
            ->key($input['chapter_id'])
            ->note('编辑分会信息')
            ->save();
        return redirect('admin_system/chapter/chapters');
    }



    /*=================================================================================
    **************  分会会长==========================================================
    ===================================================================================*/
    /**
     * 会长列表
     * */
    public function prisidents(Request $request){
        $chapterId  = $request->input('chapterId');
        $chapterModel   = new ChapterModel();
        $prisidents     = $chapterModel->get_chapter_prisidents($chapterId);
        return view('admin_system/chapter/prisidents',['prisidents'=>$prisidents,'chapterId'=>$chapterId]);
    }



    /**
     * 添加会长
     * */
    public function edit_prisident_index(){
        return view('admin_system/chapter/prisident-edit');
    }

    /**
     * 获取会长信息
     * */
    public function get_prisident_info(Request $request){
        $chapterId  = $request->input('chapterId',0);
        $prisidentId= $request->input('prisidentId',0);
        $chapterModel   = new ChapterModel();

        if($prisidentId > 0){
            $prisidentInfo  = $chapterModel->get_prisident_info($prisidentId);
        }else{
            $prisidentInfo  = $chapterModel->get_empty_prisident();
        }

        if($prisidentId > 0){
            $chapterId = $prisidentInfo->chapter_id;
        }
        $chapterInfo    = $chapterModel->chapter_info($chapterId);

        if($prisidentInfo->chapter_prisident_id > 0){
            $userModel  = new MemberModel();
            $memberInfo = $userModel->get_member_info_by_id($prisidentInfo->user_id);
            $prisidentInfo->prisidentName   = $memberInfo->YourName;
        }else{
            $prisidentInfo->prisidentName   = "";
        }
        $prisidentInfo->chapterId   = $chapterInfo->chapter_id;
        $prisidentInfo->chapterName = $chapterInfo->chapter_name;

        $apiData    = new ApiData();
        return $apiData->set_data('prisidentInfo',$prisidentInfo)->send(200,'ok');
    }

    /**
     * 删除分会会长
     * */
    public function delete_prisident(Request $request){
        $prisidentId    = $request->input('prisidentId');
        $chapterModel   = new ChapterModel();
        $chapterModel->delete_prisident($prisidentId);
        TableLog()
            ->table('chapter_prisident')
            ->key($prisidentId)
            ->content(['is_delete'=>1])
            ->note('删除会长')
            ->save();
        return redirect()->back();
    }


    /**
     * 更新/添加 会长信息
     * */
    public function update_prisident(Request $request){

        $prisidentId    = $request->input('chapter_prisident_id');
        $data           = [
            'chapter_prisident_id'  => $prisidentId,
            'chapter_id'    => $request->input('chapterId'),
            'user_id'       => $request->input('user_id'),
            'prisident_type'=> $request->input('prisident_type'),
            'total_money'   => $request->input('total_money'),
            'payed_money'   => $request->input('payed_money'),
            'surplus_money' => $request->input('surplus_money')
        ];

        $chapterModel   = new ChapterModel();
        $chapterModel->add_prisident($data);
        $apiData    = new ApiData();
        TableLog()
            ->table('chapter_prisident')
            ->key($prisidentId)
            ->content($data)
            ->note('修改会长信息')
            ->save();
        return $apiData->send(200,'操作成功');
    }



    /**=================================================================================
    **************  分会学习官 ==========================================================
    *===================================================================================*/


    /**
     * 获得分会学习官
     * */
    public function officers(Request $request){
        $chapterId      = $request->input('chapterId');
        $keywords       = trim($request->input('keywords'));
        $chapterModel   = new ChapterModel();
        $clos           = $chapterModel->get_chapter_clos($chapterId,$keywords);

        $data           = [
            'officers'  => $clos,
            'keywords'  => $keywords,
            'chapterId' => $chapterId
        ];
        return view('admin_system/chapter/officers',$data);
    }


    public function officer_edit_index(){
        return view('admin_system/chapter/officer-edit');
    }

    public function officer_edit_action(Request $request){

        $chapterModel   = new ChapterModel();
        $chapterDrpId   = $request->input('chapter_drp_id',0);
        $data = [
            'chapter_drp_id'=> $chapterDrpId,
            'chapter_id'    => $request->input('chapter_id'),
            'user_id'       => $request->input('UserID'),
            'total_money'   => $request->input('total_money'),
            'payed_money'   => $request->input('payed_money'),
            'surplus_money' => $request->input('surplus_money')
        ];
        $chapterModel->update_officer_info($data);
        table_log('chapter_drp',$chapterDrpId,$data);
        $apiData    = new ApiData();
        return $apiData->send(200,'操作成功');
    }

    /*删除学习官*/
    public function offecer_delete(Request $request)
    {

        $chapterOfficeId = $request->input('officerId');
        DB::table('chapter_drp')->where('chapter_drp_id',$chapterOfficeId)->update(['is_delete'=>1]);
        return apiData()->notice('已经移除');
    }

    /**
     * 获得学习官信息
     * */
    public function get_officer_info(Request $request)
    {
        $officerId  = $request->input('officerId',0);
        $chapterId  = $request->input('chapterId',0);

        $chapterModel   = new ChapterModel();
        if($officerId){
            $officerInfo    = $chapterModel->get_chapter_officer_info_by_officerid($officerId);
            $chapterId      = $officerInfo->chapter_id;
        }else{
            $officerInfo = new \stdClass();
            $officerInfo->chapter_drp_id    = 0;
            $officerInfo->YourName          = "";
            $officerInfo->UserID            = 0;
            $officerInfo->chapter_id        = $chapterId;
        }

        $chapterInfo    = $chapterModel->chapter_info($chapterId);
        $officerInfo->chapterName   = $chapterInfo->chapter_name;

        $apiData    = new ApiData();
        return $apiData->set_data('officerInfo',$officerInfo)
                        ->send(200,"ok");
    }




    /*=================================================================================
   **************  分会企业会员 =======================================================
   ===================================================================================*/
    /**
     * 企业会员列表
     * $param Request $request
     * */
    public function companys(Request $request){
        $chapterId      = $request->input('chapterId');
        $keywords       = trim($request->input('keywords',''));
        $chapterModel   = new ChapterModel();
        $companys       = $chapterModel->get_chapter_companys($chapterId,$keywords);
        $data           = [
            'chapterId' => $chapterId,
            'keywords'  => $keywords,
            'companys'  => $companys
        ];


        return view('admin_system/chapter/companys',$data);
    }



    /*=================================================================================
   **************  个人会员 =======================================================
   ===================================================================================*/
    public function members(Request $request){
        $chapterId      = $request->input('chapterId');
        $keywords       = $request->input('keywords','');
        $keywords       = trim($keywords);
        $chapterModel   = new ChapterModel();
        $members        = $chapterModel->get_chapter_members($chapterId,$keywords);

        $data           = [
            'members'   => $members,
            'keywords'  => $keywords,
            'chapterId' => $chapterId
        ];
        return view('admin_system/chapter/members',$data);
    }

}
